#!/usr/bin/env python3
"""
OCR项目启动脚本
"""
import os
import sys
import subprocess

def check_python():
    """检查Python版本"""
    if sys.version_info < (3, 7):
        print("错误: 需要Python 3.7或更高版本")
        return False
    return True

def create_venv():
    """创建虚拟环境"""
    if not os.path.exists('venv'):
        print("创建虚拟环境...")
        subprocess.run([sys.executable, '-m', 'venv', 'venv'])
        return True
    return False

def install_requirements():
    """安装依赖"""
    print("安装依赖包...")
    if os.name == 'nt':  # Windows
        pip_path = os.path.join('venv', 'Scripts', 'pip')
    else:  # Unix/Linux/Mac
        pip_path = os.path.join('venv', 'bin', 'pip')
    
    subprocess.run([pip_path, 'install', '-r', 'requirements.txt'])

def create_directories():
    """创建必要的目录"""
    dirs = ['uploads', 'static', 'templates']
    for dir_name in dirs:
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
            print(f"创建目录: {dir_name}")

def main():
    """主函数"""
    if not check_python():
        return
    
    print("OCR项目初始化...")
    
    # 创建虚拟环境
    venv_created = create_venv()
    
    # 安装依赖
    if venv_created or not os.path.exists(os.path.join('venv', 'lib')):
        install_requirements()
    
    # 创建目录
    create_directories()
    
    print("\n项目初始化完成!")
    print("使用以下命令启动项目:")
    if os.name == 'nt':
        print("  venv\\Scripts\\activate")
    else:
        print("  source venv/bin/activate")
    print("  python app.py")
    print("\n或者直接运行: start.bat (Windows)")

if __name__ == '__main__':
    main()